<?php


class ModuleRunner{
	private $configData =  array(
		"PersonModule"=>array(
			'person'=>'bob',

	),
		"FtpModule"=>array('host'=>'example.com',
							'user'=>'anon',
							'abc'=>'abc'
						)
	);

	private $modules = array();


	function init(){
		$interface = new ReflectionClass('Module');
		foreach($this->configData as $modulename => $params){
			$module_class = new ReflectionClass($modulename);
			if(! $module_class->isSubclassOf($interface)){
				throw new Exception("unknow module type:$modulename");
			}
			$module = $module_class->newInstance();
			foreach($module_class->getMethods() as $method){
				$this->handleMethod($module,$method,$params);
			}
			array_push($this->modules,$module);

			
		}
		return $this->modules;
	}


	function handleMethod(Module $module,ReflectionMethod $method,$params){
		$name = $method->getName();
		//var_dump($name);
		$args = $method->getParameters();

		if(count($args) != 1 || substr($name,0,3) != "set"){
			return false;
		}

		$property = strtolower(substr($name,3));
		if(!isset($params[$property])){
			return false;
		}

		$arg_class = $args[0]->getClass();
		//var_dump($arg_class);
		if(empty($arg_class)){
			//var_dump($params,$property);
			$method->invoke($module,$params[$property]);
		}else{
			$method->invoke($module,$arg_class->newInstance($params[$property]));
		}
	}
	
}

